$(function() {
	/*===图片懒加载===*/
	start();
	$(window).on('scroll', function() {
		start();
	});
	/*===图片轮播===================================================*/
	var swiperBanner = new Swiper('.index-banner-swi .swiper-container', {
		pagination: '.index-banner-swi .swiper-pagination',
		paginationClickable: '.index-banner-swi .swiper-pagination',
		autoplay: 3000,
		autoplayDisableOnInteraction: false,
		effect: 'effect'
	});
	var profileSwi = new Swiper('.profileSwi .swiper-container', {
		pagination: '.profileSwi .swiper-pagination',
		paginationClickable: '.profileSwi .swiper-pagination',
		autoplay: 3000,
		autoplayDisableOnInteraction: false,
		effect: 'effect'
	});
	/*===公告滚动===================================================*/
	setInterval('AutoScroll("#scroll")', 3000);
	/*===城市定位===================================================*/
	if ($("#positon").length > 1) {
		var latlon = null;
		//ajax获取用户所在经纬度
		$.ajax({
			url: "https://api.map.baidu.com/location/ip?ak=YZQRHxzAIRKXF3qk7O1qWQkHf5lqoa9y&coor=bd09ll",
			type: "POST",
			dataType: "jsonp",
			success: function(data) {
				latlon = data.content.point.y + "," + data.content.point.x;
				//ajax根据经纬度获取省市区
				$.ajax({
					type: "POST",
					dataType: "jsonp",
					url: 'https://api.map.baidu.com/geocoder/v2/?ak=YZQRHxzAIRKXF3qk7O1qWQkHf5lqoa9y&callback=renderReverse&location=' + latlon + '&output=json&pois=0',
					success: function(json) {
						if (json.status == 0) {
							//console.log(json.result.addressComponent.province+"-"+json.result.addressComponent.city+"-"+json.result.addressComponent.district);
							var city = json.result.addressComponent.city;
							$("#positon").text(city);
						}
					}
				});
			}
		});
	}
	/*===星级===================================================*/
	var star = parseInt($("#stars").attr("data-star"));
	$("#stars .star").each(function(){
		if($(this).index()<star){
			$(this).addClass("active");
		}
	})
	/*===弹窗===================================================*/
	$("#TcCloseBtn").click(function(){
		$("#tc").stop().fadeOut();
	})
	/*===日期选择===================================================*/	
	if($("#datePick").length>0){
		var date = new Date();
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var day = date.getDate();
		$("#datePick").text(year + '-' + month + '-' + day);
		new Rolldate({
			el: '#datePick',
			format: 'YYYY-MM-DD',
			beginYear: 2000,
			endYear: 2100,		
		})
	}
	/*===档期表单提交===================================================*/	
	$("#addformSub").click(function(){
		var flag = true;
		var telReg = /(1[3-9]\d{9}$)/;
		$("form .required").each(function() {
			if (!$(this).val()) {
				layer.msg($(this).attr("msg"));
				flag = false;
				return false;
			}
		});	
		if (flag) {
			$("form .tel").each(function() {
				if (!telReg.test($(this).val())) {
					layer.msg($(this).attr("telmsg"));
					flag = false;
					return false;
				}
			});
		};
		if(flag){
			$.ajax({
				url: '',
				type: "post",
				data: {
					
				},
				dataType: 'json',
				success: function(data) {
					if (data.code == '1') {
						layer.msg(data.msg);
					} else {
						layer.msg(data.msg);
					}
				},
				error: function() {
					layer.msg("网络连接错误!");
				}
			})
		}
	})
	/*===成为人才===================================================*/
	if($(".formMo").length>0){
		/*===成为人才--发送验证码===*/
		var countdown = 60;
		$("#yzmBtn").click(function() {
			var mobile = $("#mobile").val();
			if ($(this).hasClass("disabled")) {
				return false;
			}
			if (!mobile) {
				layer.msg("手机号不能为空!");
				return false;
			}
			var reg = /(1[3-9]\d{9}$)/;
			if (!reg.test(mobile)) {
				layer.msg("手机号格式不正确!");
				return false;
			}
			var obj = $("#yzmBtn");
			settime(obj, countdown);
		
			//后台发送验证码
			$.ajax({
				url: '',
				data: {
					'mobile': mobile
				},
				success: function(data) {}
			});
		
		});
		
		/*===判断是否有地址====*/
		var per_province = $("#per_province").val() ? $("#per_province").val() : "四川省";
		var per_city = $("#per_city").val() ? $("#per_city").val() : "成都市";
		/*===成为人才--省市选择===*/
		$('#distpicker').distpicker({
			province: per_province,
			city: per_city
		});
		/*===成为人才--图片上传===*/
		$('#picUpload').diyUpload({
			url: '',
			success: function(data) {
				var str = data.data.src;
				var img_y = $("#images").val();
				$("#images").val(img_y + "#" + str);
			},
			error: function(err) {
		
			}
		});
		/*===成为人才--视频上传===*/
		$('#videoUpload').diyUpload({
			url: '',
			success: function(data) {
				var str = data.filePath;
				var video_y = $("#video").val();
				$("#video").val(video_y + "#" + str);
			},
			error: function(err) {
		
			},
			buttonText: '+ 添加视频',
			chunked: false,
			// 分片大小
			chunkSize: 512 * 1024,
			//最大上传的文件数量, 总文件大小,单个文件大小(单位字节);
			fileNumLimit: 1,
			fileSizeLimit: 50000 * 1024,
			fileSingleSizeLimit: 50000 * 1024,
			accept: {
				extensions: 'mp4',
			}
		});
		/*===成为人才--表单提交===*/
		$(".talentForm .formSub").click(function() {
			var name = $("#name").val();
			var mobile = $("#mobile").val();
			var yzm = $("#yzm").val();
			var province = $("#province").val();
			var city = $("#city").val();
			var type = $(".checkbox:checked").val();
			var type='';
			$(".checkbox:checked").each(function(){
				type += $(this).val()+',';
			})
			// console.log(type);	
			var profile = $("#profile").val();
			var video = $("#video").val();
			var images = $("#images").val();
			var openid = $("#openid").val();
			var id = $("#id").val();
			if (!name) {
				layer.msg("姓名不能为空!");
				return false;
			}
			if (!mobile) {
				layer.msg("电话不能为空!");
				return false;
			}
			var reg = /(1[3-9]\d{9}$)/;
			if (!reg.test(mobile)) {
				layer.msg("手机号格式不正确!");
				return false;
			}
			if (!yzm) {
				layer.msg("验证码不能为空!");
				return false;
			}
			if (!province || !city) {
				layer.msg("省市不能为空!");
				return false;
			}
			if (!type) {
				layer.msg("分类不能为空!");
				return false;
			}
			if (!profile) {
				layer.msg("自我介绍不能为空!");
				return false;
			}
			//if(!video) {
			//layer.msg("视频没有上传!");
			//return false;
			//}
			if (!images) {
				layer.msg("图片没有上传!");
				return false;
			}
			
			$.ajax({
				url: '',
				type: "post",
				data: {
					
				},
				dataType: 'json',
				success: function(data) {
					if (data.code == "1") {
						
					} else {
						layer.msg(data.msg);
					}
				},
				error: function() {
					layer.msg("网络连接错误!");
				}
			})
		});
		/*===成为人才--图片读取===*/
		var picVal = $("#images").val();
		var picArr = picVal.split("#");
		var picNum = picArr.length;
		var html = '';
		
		for (var i = 1; i < picNum; i++) {
			html += '<li class="item"><img src="' + picArr[i] +
				'" class="objectCover" data-isloaded="1"><div class="delBtn"></div></li>'
		}
		$(".talentForm .picLists").html(html);
		/*===成为人才--图片删除===*/
		$(".talentForm .picLists").on('click', '.delBtn', function(e) {
			var picVal = $("#images").val();
			var delPic = '#' + $(this).prev("img").attr("src");
			$(this).parent(".item").remove();
			picVal = picVal.replace(delPic, '');
			$("#images").val(picVal);
			console.log($("#images").val());
		});
	}
	/*===点击关注===*/
	$(".collectBtn").on("click", function() {
		var cid = $(this).attr("cid");
		var is = $(this).attr("is");
		if (is == "1") {
			$.ajax({
				url: '',
				type: "post",
				data: {
					"cid": cid,
					"is": is
				},
				dataType: 'json',
				success: function(data) {
					if (data.code == '1') {
						layer.msg(data.msg);
						$(".collectBtn ").attr("is", "0");
						$(".collectBtn ").removeClass("active");
					} else {
						layer.msg(data.msg);
					}
				},
				error: function() {
					layer.msg("网络连接错误!");
				}
			})
		} else {
			$.ajax({
				url: '/index/collect/add',
				type: "post",
				data: {
					"cid": cid,
					"is": is
				},
				dataType: 'json',
				success: function(data) {
					if (data.code == '1') {
						layer.msg(data.msg);
						$(".collectBtn ").attr("is", "1");
						$(".collectBtn ").addClass("active");
					} else if (data.code == '-1') {
						layer.msg(data.msg);
						window.location.href = "/index/login/index";
					} else {
						layer.msg(data.msg);
					}
				},
				error: function() {
					layer.msg("网络连接错误!");
				}
			})
		}
	});
});
/*===返回时刷新返回页面===*/
function refresh() {
	window.location.href = document.referrer;
}
/*===图片懒加载===*/
function start() {
	$('img').not('[data-isLoaded]').each(function() {
		var $node = $(this)
		if (isShow($node)) {
			loadImg($node)
		}
	})
}

function isShow($node) {
	return $node.offset().top <= $(window).height() + $(window).scrollTop();
}

function loadImg($img) {
	$img.attr('src', $img.attr('data-src'));
	$img.attr('data-isLoaded', 1);
}
/*===发送验证码倒计时===*/
function settime(obj, time) {
	var countdown = time;
	if (countdown == 0) {
		obj.removeClass('disabled');
		obj.text("获取验证码");
		countdown = 60;
		return;
	} else {
		obj.addClass('disabled');
		obj.text("重新发送(" + countdown + ")");
		countdown--;
	}
	setTimeout(function() {
		settime(obj, countdown)
	}, 1000)
}
/*===公告滚动===*/
function AutoScroll(obj) {
	$(obj).find("ul:first").animate({
		marginTop: "-0.34rem"
	}, 500, function() {
		$(this).css({
			marginTop: "0px"
		}).find("li:first").appendTo(this);
	});
}
/*===失败理由===*/
function showTc(msg){
	$("#tc .txt").text(msg);
	$("#tc").stop().fadeIn();
}